//
//  CalMath.swift
//  
//
//  Created by zcj on 2024/12/27.
//

import Foundation
import CryptoKit
public class Cal{
    public init(){
        print("fun")
    }
    public func add(x:Int,y:Int) -> Int{
        return x + y
    }

    public func StringAppendWithPlus(strings:String...,symbol:String ) -> String{
        var resStr = ""
        for str in strings{
            resStr += "\(str)\(symbol)"
        }
        //删除最后一个 symbol
        resStr.removeLast()
        return resStr
    }
    
    //rkdbhash
    public static func hashOfString(str:String) -> Int64 {
        var seed:Int32 = 131
        var hash:Int32 = 0
        str.forEach { ch in
            let num = ch.unicodeScalars.first!.value
            hash = seed * seed + Int32(num)
        }
        return Int64((hash & 0x7FFFFFFF))
    }
    
    public static func hashOfStringSha256(str:String) -> Int64{
        let data = str.data(using: .utf8)
        let hash = SHA256.hash(data: data!)
        // 提取前 8 个字节并转换为 Int64（大端序）
        return hash.withUnsafeBytes { $0.prefix(8).load(as: Int64.self) }
    }
    static public func hashObIden(iden:ObjectIdentifier) -> Int64{
        let hashValue = iden.hashValue
        return Int64(hashValue)
    }
}
